package org.mozhu.mboot.core.i18n;

import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.core.web.HttpServletRequestSkipper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * Interceptor that allows for changing the current locale on every request, via
 * a configurable request parameter (default parameter name: "locale").
 */
@Slf4j
public class LocaleChangeInterceptor extends org.springframework.web.servlet.i18n.LocaleChangeInterceptor {
    private Logger logger = LoggerFactory.getLogger(org.springframework.web.servlet.i18n.LocaleChangeInterceptor.class);
    private Set<String> exclusions;
    private Set<String> inclusions;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
        HttpServletRequestSkipper skipper = new HttpServletRequestSkipper(exclusions, inclusions);
        if (skipper.needSkip(request)) {
            logger.debug("Skip SevletPath [{}]", request.getServletPath());
            return true;
        }
        return super.preHandle(request, response, handler);
    }

    public void setExclusions(Set<String> exclusions) {
        this.exclusions = exclusions;
    }

    public void setInclusions(Set<String> inclusions) {
        this.inclusions = inclusions;
    }
}